﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class Zone : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected CheckBox chkSysCreate;
        protected DropDownList ddlVeriMethod;
        protected HtmlForm F2131;
        protected HtmlInputHidden hdnFrom;
        protected HtmlImage imgDefaultPickToLoc;
        protected HtmlImage imgInLoc;
        protected HtmlImage imgOutLoc;
        protected Label lblDefaultPickToLoc;
        protected Label lblDesc;
        protected Label lblInLoc;
        protected Label lblMsg;
        protected Label lblOutLoc;
        protected Label lblOwner;
        protected Label lblVeriMethod;
        protected Label lblZoneID;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDefaultPickToLoc;
        protected TextBox txtDesc;
        protected TextBox txtInLoc;
        protected TextBox txtOutLoc;
        protected TextBox txtZoneID;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtZoneID.Text = "";
            this.txtZoneID.Enabled = true;
            this.chkSysCreate.Checked = false;
            this.imgDefaultPickToLoc.Visible = true;
            this.imgInLoc.Visible = true;
            this.imgOutLoc.Visible = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.chkSysCreate.Checked)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C011");
                }
                else if (this.txtZoneID.Text.Trim() != "")
                {
                    List<string> zoneID = new List<string> {
                        this.txtZoneID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(zoneID, "ZONE") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("ZoneQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("ZoneList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.txtZoneID.Text.Trim() == "")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2310_001");
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateZoneInfo();
            }
            else
            {
                this.InsertZoneInfo();
            }
        }

        private void InitDropDownList()
        {
            base.BindData2DDLBySysCode(this.ddlVeriMethod, "LocVerMethod");
        }

        private void InsertZoneInfo()
        {
            try
            {
                ZoneInfo zoneInfo = new ZoneInfo {
                    ZoneID = this.txtZoneID.Text.Trim(),
                    PickToLoc = this.txtDefaultPickToLoc.Text.Trim(),
                    Descr = this.txtDesc.Text.Trim(),
                    InLoc = this.txtInLoc.Text.Trim(),
                    OutLoc = this.txtOutLoc.Text.Trim(),
                    VerificationMethod = this.ddlVeriMethod.SelectedValue,
                    IsActive = this.chkIsActive.Checked ? "Y" : "N",
                    SystemCreated = this.chkSysCreate.Checked ? "Y" : "N",
                    Uom1PickMethod = "",
                    Uom2PickMethod = "",
                    Uom3PickMethod = "",
                    Uom4PickMethod = "",
                    Uom5PickMethod = "",
                    Uom6PickMethod = "",
                    ReplenishmentMethod = "",
                    CreateAssignments = "",
                    ZoneBreak = "",
                    MaxPickingContainers = 0,
                    LaborMaxCasecnt = 0,
                    LaborMaxCube = 0,
                    LaborMaxWeight = 0,
                    AisleEnd = 0,
                    AisleStart = 0,
                    SlotEnd = 0,
                    SlotStart = 0,
                    CreatedBy = base.UserInfo.UserID
                };
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(zoneInfo, "ZONE") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = zoneInfo;
                this.txtZoneID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            this.InitDropDownList();
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetZoneInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetZoneInfo2Form()
        {
            try
            {
                string zoneID = base.Request.QueryString["ID"].ToString();
                ZoneInfo zoneInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(zoneID, "ZONE") as ZoneInfo;
                base.DataEntityViewState = zoneInfo;
                if (zoneInfo != null)
                {
                    this.txtZoneID.Text = zoneInfo.ZoneID;
                    this.txtDefaultPickToLoc.Text = zoneInfo.PickToLoc.ToString();
                    this.txtDesc.Text = zoneInfo.Descr;
                    this.txtInLoc.Text = zoneInfo.InLoc.ToString();
                    this.txtOutLoc.Text = zoneInfo.OutLoc.ToString();
                    this.ddlVeriMethod.SelectedValue = zoneInfo.VerificationMethod;
                    this.chkIsActive.Checked = zoneInfo.IsActive == "Y";
                    this.chkSysCreate.Checked = zoneInfo.SystemCreated == "Y";
                    this.txtZoneID.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateZoneInfo()
        {
            try
            {
                ZoneInfo zoneInfo = base.DataEntityViewState as ZoneInfo;
                zoneInfo.PickToLoc = this.txtDefaultPickToLoc.Text.Trim();
                zoneInfo.Descr = this.txtDesc.Text.Trim();
                zoneInfo.InLoc = this.txtInLoc.Text.Trim();
                zoneInfo.OutLoc = this.txtOutLoc.Text.Trim();
                zoneInfo.VerificationMethod = this.ddlVeriMethod.SelectedValue;
                zoneInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                zoneInfo.SystemCreated = this.chkSysCreate.Checked ? "Y" : "N";
                zoneInfo.UpdatedBy = base.UserInfo.UserID;
                zoneInfo.Uom1PickMethod = "";
                zoneInfo.Uom2PickMethod = "";
                zoneInfo.Uom3PickMethod = "";
                zoneInfo.Uom4PickMethod = "";
                zoneInfo.Uom5PickMethod = "";
                zoneInfo.Uom6PickMethod = "";
                zoneInfo.ReplenishmentMethod = "";
                zoneInfo.CreateAssignments = "";
                zoneInfo.ZoneBreak = "";
                zoneInfo.MaxPickingContainers = 0;
                zoneInfo.LaborMaxCasecnt = 0;
                zoneInfo.LaborMaxCube = 0;
                zoneInfo.LaborMaxWeight = 0;
                zoneInfo.AisleEnd = 0;
                zoneInfo.AisleStart = 0;
                zoneInfo.SlotEnd = 0;
                zoneInfo.SlotStart = 0;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(zoneInfo, "ZONE") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

